package com.project.finance.system.controller;

import com.project.finance.common.po.TbSysUser;
import com.project.finance.common.po.TbUsers;
import com.project.finance.common.resultconfig.Result;
import com.project.finance.common.resultconfig.ResultGenerator;
import com.project.finance.common.vo.ResultMsg;
import com.project.finance.system.service.SystemService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/*
 * 系统管理人员crud
 *
 */
@Slf4j
@RestController
@RequestMapping("/api")
public class SystemController {
    private static final Logger logger = LogManager.getLogger(SystemController.class);

    @Autowired
    private SystemService systemService;


    /*
     * 登录
     */
    @RequestMapping(value = "/dealLogin", method = RequestMethod.POST)
    public Result dealLogin(@RequestBody TbSysUser user) {
            log.info("user======"+user);
//        TbSysUser sysUser = new TbSysUser();
//        sysUser.setLoginname(user.getLoginname());
//        sysUser.setPassword(user.getPassword());
        //认证过程
        //创建令牌
        UsernamePasswordToken token = new UsernamePasswordToken(user.getLoginname(),user.getPassword());
        //创建主体对象
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            if (subject.isAuthenticated()){
                String loginname = user.getLoginname();
                return ResultGenerator.apiSuccess(loginname);
            }
        } catch (AuthenticationException e) {
            e.printStackTrace();
        }
        return ResultGenerator.apiError();


    }

    /**
     * 查询所有系统用户信息
     *
     * @return
     */
    @RequestMapping(value = "sysusers", method = RequestMethod.GET)
    public Result listSysUsers() {
        List<TbSysUser> tbSysUsers = systemService.listSysUsers();
        return ResultGenerator.apiSuccess(tbSysUsers);
    }

    /**
     * 根据 账号 来查询管理员用户
     *
     * @param loginName
     * @return
     */
    @RequestMapping(value = "sysuser", method = RequestMethod.GET)
    public Result getSysUser(@RequestParam("name") String loginName) {
        logger.info("GET Name : " + loginName);
        List<TbSysUser> sysUser = systemService.getSysUser(loginName);
        Result tbSysUserResult = ResultGenerator.apiSuccess(sysUser);
        return tbSysUserResult;
    }

    /**
     * 添加管理员用户
     *
     * @param tbSysUser
     * @return
     */
    @RequestMapping(value = "sysuser", method = RequestMethod.POST)
    public Result saveSysUser(@RequestBody TbSysUser tbSysUser) {
        int i = systemService.saveSysUser(tbSysUser);
        if (i > 0) {  // 添加成功
            return ResultGenerator.apiSuccess("ok");
        } else {
            return ResultGenerator.apiError("error");
        }
    }

    /**
     * 通过 主键 进行修改管理员用户信息
     *
     * @param tbSysUser
     * @return
     */
    @RequestMapping(value = "/sysuser", method = RequestMethod.PUT)
    public Result updateSysUser(@RequestBody TbSysUser tbSysUser) {
        logger.info("PUT : " + tbSysUser);
        int i = systemService.updateSysUser(tbSysUser);
        if (i > 0) {
            return ResultGenerator.apiSuccess("ok");
        } else {
            return ResultGenerator.apiError("error");
        }
    }

    /**
     * 根据id 删除 用户管理员信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/sysuser", method = RequestMethod.DELETE)
    public Result deleteSysUser(@RequestParam("id") int id) {
        logger.info("DELETE ID : " + id);
        int i = systemService.deleteSysUser(id);
        if (i > 0) {
            return ResultGenerator.apiSuccess("ok");
        } else {
            return ResultGenerator.apiError("error");
        }
    }

    //分页
    @RequestMapping(value = "/sysUser", method = RequestMethod.GET)
    public ResultMsg listSysUserByPageCondition(@RequestParam("pageSize") int pageSize,
                                                @RequestParam("pageNum") int pageNum) {
        ResultMsg resultMsg = systemService.listSysUserByPage(pageSize, pageNum);
        resultMsg.setCode(0);
        return resultMsg;
    }
}
